local skel = fk.CreateSkill {
  name = "lb_zero__xunshang",
  max_branches_use_time = {
    ["phase"] = {
      [Player.HistoryPhase] = 1,
    }
  }
}

local U = require "packages.utility.utility"

Fk:loadTranslationTable {
  ["lb_zero__xunshang"] = "循殇",
  [":lb_zero__xunshang"] = "每阶段限一次，一张非转化实体牌结算后，你可以弃置一张伤害牌，视为使用上一张被使用的牌，若你无法使用或此牌不为基本牌或普通锦囊牌，你失去1点体力。",

  ["#lb_zero__xunshang"] = "循殇：你可以弃置一张伤害牌，视为使用 %arg ，若无法使用则失去1点体力", 
  ["#lb_zero__xunshang_lose"] = "循殇：你可以弃置一张伤害牌，然后失去1点体力",  -- 直接提示扣血算了

  ["$lb_zero__xunshang1"] = "心无他物，方可命中！",
  ["$lb_zero__xunshang2"] = "只需一瞬足矣！",
}

skel:addEffect(fk.CardUseFinished, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and not player:isNude() and U.isPureCard(data.card)
    and skel:withinBranchTimesLimit(player, "phase", Player.phase)
    and table.find(player:getCardIds("he"), function(id)
      return Fk:getCardById(id).is_damage_card
    end)
  end,
  on_cost = function (self, event, target, player, data)
    local lastUseEvent = player.room.logic:getEventsByRule(GameEvent.UseCard, 1, function(e)
      return e.id ~= (player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true) or {}).id
      and #e.data.tos > 0
    end, 0)[1]
    if lastUseEvent == nil then return false end
    local use_card = lastUseEvent.data.card
    local name = use_card.name
    local prompt = "#lb_zero__xunshang:::" .. name
    if not (use_card.type == Card.TypeBasic or use_card:isCommonTrick()) then
      prompt = "#lb_zero__xunshang_lose"
      name = ""
    end
    local damage_card = table.filter(player:getCardIds("he"), function(id)
      return Fk:getCardById(id).is_damage_card
    end)
    local pattern = ".|.|.|.|.|.|" .. table.concat(damage_card, ",")
    local cards = player.room:askToDiscard(player, {
      min_num = 1,
      max_num = 1,
      pattern = pattern,
      include_equip = true,
      skill_name = skel.name,
      prompt = prompt,
      skip = true,
    })
    if #cards > 0 then
      event:setCostData(self, {cards = cards, name = name})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    player:addSkillBranchUseHistory(skel.name, "phase")
    local room = player.room
    room:throwCard(event:getCostData(self).cards, skel.name, player)
    if player.dead then return end
    local name = event:getCostData(self).name
    if name == "" or not room:askToUseVirtualCard(player, { name = name, skill_name = skel.name, cancelable = false, }) then
      room:loseHp(player, 1, skel.name)
    end
  end,
}, { check_skill_limit = true })

return skel